package com.example.network_sdk

import com.zyj.retrofit.adapter.FlowCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

abstract class BaseHttpRetrofitImpl : HttpRetrofit{
    protected fun createOkHttpBuilder() :OkHttpClient.Builder{
        val builder = OkHttpClient.Builder()
        val httpLoggingInterceptor = HttpLoggingInterceptor()
        httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        builder.addInterceptor(httpLoggingInterceptor)
        builder.readTimeout(readTime(), TimeUnit.SECONDS)
        builder.connectTimeout(connectTime(), TimeUnit.SECONDS)
        builder.writeTimeout(writeTime(), TimeUnit.SECONDS)
        return builder
    }
    protected abstract fun readTime() : Long
    protected abstract fun connectTime() : Long
    protected abstract fun writeTime() : Long

    override fun createRetrofit(client: OkHttpClient): Retrofit {
        val builder = Retrofit.Builder()
        builder.client(client)
            .baseUrl("http://118.195.161.134:8077/")
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
        return builder.build()
    }
}